package com.example.smartcitydemo.ui.home.activity

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.DividerItemDecoration
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.IP
import com.example.smartcitydemo.MyApplication
import com.example.smartcitydemo.NEWS_DETAIL
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseActivity
import com.example.smartcitydemo.bean.NewsCommitBean
import com.example.smartcitydemo.bean.ReplyInfo
import com.example.smartcitydemo.databinding.ActivityNewsDetailBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ui.home.MyReplyAdapter
import com.example.smartcitydemo.util.EasyImageGetter
import com.example.smartcitydemo.viewModel.home.HomeViewModel
import com.wx.goodview.GoodView

class NewsDetailActivity : BaseActivity<ActivityNewsDetailBinding>(R.layout.activity_news_detail) {
    val homeViewModel: HomeViewModel by viewModels()
    var newsDetailId: Int = 0
    val myGetter: EasyImageGetter = EasyImageGetter.create().apply {
        setLoader {
            val wholeString = "${MyApplication.sp.getString(IP, "")}$it"
            Glide.with(this@NewsDetailActivity).load(wholeString).submit().get()
        }
        setPlaceHolder(R.drawable.loading)
        setError(R.drawable.picfail)
    }

    val replyAdapter: MyReplyAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        MyReplyAdapter().apply {
            init()
            setDiffCallback(ReplyInfo.callback)
        }
    }
    val goodView: GoodView by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        GoodView(this).apply {
            setTextInfo("点赞+1", resources.getColor(R.color.colorAccent), 12)
            setAlpha(0f, 1f)
        }
    }


    override fun addObserver(contentView: ActivityNewsDetailBinding) {
        homeViewModel.newsDetailData.observe(this) {
            Log.d(TAG, "addObserver: $it")
            when (it) {
                is Resource.Success -> {
                    contentView.data = it.data
                    myGetter.loadHtml(it.data?.content ?: "", contentView.tvNewsContent)
                }
            }
        }
        homeViewModel.doLikeResult.observe(this) {
            Log.d(TAG, "addObserver: $it")
            if (it is Resource.Success) {
                goodView.show(contentView.ivLike)
                val likeNumString = contentView.tvLikeButton.text.trim().toString()
                var likeNum: Int = likeNumString.toInt()
                ++likeNum;
                contentView.tvLikeButton.text = "$likeNum"
            }
        }
        homeViewModel.newsReplyInfo.observe(this) {
            when (it) {
                is Resource.Success -> {
                    replyAdapter.setDiffNewData(it.data!!.toMutableList())
                }
            }
        }

        homeViewModel.likeCommitResult.observe(this) {
            when (it) {
                is Resource.Success -> {

                }
            }
        }
        homeViewModel.submitResult.observe(this) {
            Log.d(TAG, "addObserver: $it")
            if (it is Resource.Success) {
                //刷新数据
                homeViewModel.getNewsReply(newsDetailId)
            }
        }
    }

    override fun initData() {
        newsDetailId = intent.getIntExtra(NEWS_DETAIL, 0)
        homeViewModel.getNewsDetail(newsDetailId)
        homeViewModel.getNewsReply(newsDetailId)
    }

    override fun initView(contentView: ActivityNewsDetailBinding, savedInstanceState: Bundle?) {
        contentView.run {
            homeNewsToolbar.tvContent.text = "新闻详情"
            commitTypeBar.typeBarText.text="评论列表"
            ivLike.setOnClickListener {
                val newsDetailId = it.tag as Int
                homeViewModel.doLikeArticle(newsDetailId)
            }
            commitRecycler.addItemDecoration(
                DividerItemDecoration(
                    commitRecycler.context,
                    DividerItemDecoration.VERTICAL
                )
            )
            commitRecycler.adapter = replyAdapter
            replyAdapter.setOnItemClickListener { adapter, view, pos ->
                val replyId = view.tag as Int
                val findViewById = view.findViewById<TextView>(R.id.tv_like_num)
                val toString = findViewById.text.trim().toString()
                var toInt = toString.toInt()
                ++toInt
                findViewById.text = "$toInt"
                goodView.show(view)
                homeViewModel.doLikeCommit(replyId)
            }
            newsInputBottomBar.submitButton.setOnClickListener {
                val toString = newsInputBottomBar.commitInput.text!!.trim().toString()
                if (toString.isEmpty()) {
                    ToastUtils.showShort("请输入内容")
                } else {
                    homeViewModel.submitCommit(NewsCommitBean(toString, newsDetailId))
                    newsInputBottomBar.commitInput.setText("")
                    KeyboardUtils.hideSoftInput(it)
                }
            }
        }
    }


}